<?php
/**
 * Groo BookTest Class.
 *
 * $Horde: incubator/groo/lib/BookTest.php,v 1.2 2006/08/11 04:39:23 luciano Exp $
 *
 * Copyright 2004-2006 Luciano Ramalho <luciano@ramalho.org>
 *
 * See the enclosed file COPYING for license information (GPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
 *
 * @author  Luciano Ramalho <luciano@ramalho.org>
 * @package Groo
 */


// Call BookTest::main() if this source file is executed directly.
if (!defined("PHPUnit2_MAIN_METHOD")) {
    define("PHPUnit2_MAIN_METHOD", "BookTest::main");
}

require_once "PHPUnit2/Framework/TestCase.php";
require_once "PHPUnit2/Framework/TestSuite.php";

require_once "Book.php";

class BookTest extends PHPUnit2_Framework_TestCase {
    public static function main() {
        require_once "PHPUnit2/TextUI/TestRunner.php";

        $suite  = new PHPUnit2_Framework_TestSuite("BookTest");
        $result = PHPUnit2_TextUI_TestRunner::run($suite);
    }

    protected function setUp() {
        $this->alice_xml = '<?xml version="1.0" encoding="utf-8"?>'."\n<book>"
                        .'<title>The Annotated Alice: The Definitive Edition</title>'
                        .'<edition>Updated</edition>'
                        .'<publisher>W. W. Norton &amp; Company</publisher>'
                        .'<issued>1999-11</issued>'
                        .'<isbn13>9780393048476</isbn13>'
                        .'<creator role="author">Lewis Carroll</creator>'
                        .'<creator role="editor">Martin Gardner</creator>'
                        .'<creator role="illustrator">John Tenniel</creator>'
                        .'</book>';

        $this->gof_xml = '<?xml version="1.0" encoding="utf-8"?>'."\n"
                        .'<book><title>Design Patterns</title>'
                        .'<publisher>Addison Wesley</publisher>'
                        .'<isbn13>9780201633610</isbn13>'
                        .'<creator role="author">Erich Gamma</creator>'
                        .'<creator role="author">Richard Helm</creator>'
                        .'<creator role="author">Ralph Johnson</creator>'
                        .'<creator role="author">John Vlissides</creator>'
                        .'</book>';				
        $this->digits10ok  = '8535217142';
        $this->digits10nok = '8535217140';
        $this->digits13ok  = '9788535217148';
        $this->digits13nok = '9788535217140';
        $this->book10ok  = new Book(array('title'=>'ISBN-10 book', 'isbn'=>$this->digits10ok));
        $this->book13ok  = new Book(array('title'=>'ISBN-13 book', 'isbn'=>$this->digits13ok));
        $this->alice = new Book('Alice in Wonderland');
        $fields = array
            ('title'=>'Design Patterns'
            ,'creators'=>array
                (array('name'=>'Erich Gamma','role'=>'author')
                ,array('name'=>'Richard Helm','role'=>'author')
                ,array('name'=>'Ralph Johnson','role'=>'author')
                ,array('name'=>'John Vlissides','role'=>'author')
                )
            ,'publisher'=>'Addison Wesley'
            ,'isbn'=>'9780201633610'
            );
        $this->gof = new Book($fields);
        $fields = array
            ('title'=>'Hornblower during the crisis'
            ,'author'=>'C. S. Forester'
            ,'publisher'=>'Back Bay Books'
            ,'isbn'=>'0-316-28944-2'
            );
        $this->hdtc = new Book($fields);
    }

    public function testConstructor() {
        $fields = array
            ('title'=>'The Annotated Alice'
            ,'author'=>'Lewis Carroll'
            ,'creators'=>array
                (array('name'=>'John Tenniel','role'=>'illustrator')
                ,array('name'=>'Martin Gardner','role'=>'editor')
                )
            ,'publisher'=>'W. W. Norton & Co.'
            ,'edition'=>'The Definitive Edition'
            ,'issued'=>'1987-12'
            ,'isbn'=>'0393048470'
            );
        $alice2 = new Book($fields);
        try {
            $this->book10nok = new Book(array('title'=>'Invalid ISBN-10','isbn'=>$this->digits10nok));
        } catch (Exception $expected) { return; }
        try {
            $this->book13nok = new Book(array('title'=>'Invalid ISBN-13','isbn'=>$this->digits10nok));
        } catch (Exception $expected) { return; }
        $this->fail('Book constructor should not accept an invalid ISBN');
    }
    
    public function testHasValidISBN() {
        $this->assertTrue($this->book10ok->hasValidISBN());
        $this->assertTrue($this->book13ok->hasValidISBN());
        $this->assertTrue($this->gof->hasValidISBN());
        $this->assertTrue($this->hdtc->hasValidISBN());
        $this->assertFalse($this->alice->hasValidISBN());
    }
    
    public function testGetISBN10() {
        $this->assertEquals('0316289442',$this->hdtc->getISBN(10));    
        $this->assertEquals($this->digits10ok,$this->book13ok->getISBN(10));    
    }

    public function testSetISBN() {
        $this->alice->setISBN(' \t 0 448 06004-3 \n ');   
        $this->assertEquals('0448060043',$this->alice->getISBN(10));    
        try {
            $this->alice->setISBN('0-448-06004-');   
        } catch (Exception $expected) { return; }
        $this->fail('Book constructor should not accept an invalid ISBN');
    }

    public function testToXML() {
        $this->assertEquals(trim($this->gof_xml), trim($this->gof->toXML()));    
        $alice = Book::fromXML($this->alice_xml);
        $this->assertEquals(trim($this->alice_xml), trim($alice->toXML()));
    }

    public function testFromXML() {
        $gof = Book::fromXML($this->gof_xml);
        $this->assertEquals('Design Patterns', $gof->title);
        $this->assertEquals('Addison Wesley', $gof->publisher);
        $this->assertEquals('9780201633610', $gof->getISBN(13));
        $alice = Book::fromXML($this->alice_xml);
        $xml = $alice->toXML();
        $this->assertEquals(trim($this->alice_xml), trim($xml));
    }
    
    public function testGetSortTitle() {
        $alice = new Book('The Annotated Alice: The Definitive Edition');
        $aliceSortTitle = 'Annotated Alice: The Definitive Edition';
        $gof = new Book('Design Patterns');
        $gofSortTitle = 'Design Patterns';
        $night = new Book('A Night to Remember');
        $nightSortTitle = 'Night to Remember';
        $this->assertEquals($aliceSortTitle, $alice->getSortTitle());
        $this->assertEquals($gofSortTitle, $gof->getSortTitle());
        $this->assertEquals($nightSortTitle, $night->getSortTitle());
    }
    
}

// Call BookTest::main() if this source file is executed directly.
if (PHPUnit2_MAIN_METHOD == "BookTest::main") {
    BookTest::main();
}
?>
